# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.hashers import make_password
from django.contrib.auth import authenticate, login, logout
from django.http import JsonResponse
from .froms import LoginForm,Reset_passwordFrom,Client_loginFrom
from django.shortcuts import render, redirect, reverse
from .models import Client,Admin,Personnel,Personnel_type
import hashlib
# Create your views here.


def register_admin(request):
    if request.method == 'GET':
        admin = Admin()
        admin.username = 'shuzheng'
        admin.password = make_password('123456789')
        admin.phone = 158891177167
        admin.qq = 492396868
        admin.email = '492396868@qq.com'
        admin.save()
        return redirect('account/cms_login')

def cms_login(request):
    if request.method == 'GET':

        return render(request, 'account/login.html')
    else:
        form = LoginForm(request.POST)

        if form.is_valid():
            username = form.cleaned_data.get('username', None)
            password = form.cleaned_data.get('password', None)
            print password, username

            user = authenticate(username=username, password=password)
            if user:
                login(request, user)
                return redirect('/results/index')
            else:
                return render(request, 'account/login.html', {'error': u'用户名或密码错误!'})
        else:

            return render(request, 'account/login.html', {'error': u'数据验证不通过'})


def cms_logout(request):
    logout(request)
    return redirect(reverse('account/cms_login'))




def client_register(request):
    if request.method == 'GET':
        return render(request, 'account/client_register.html')
    else:
        email = request.POST.get('email', None)
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        remember = request.POST.get('repassword', None)
        phone = request.POST.get('phone', None)
        money = request.POST.get('money', None)
        gender = request.POST.get('gender', None)
        qq_number = request.POST.get('qq_number', None)
        hash = hashlib.md5()
        hash.update(bytes(password, encoding='utf-8'))
        password = hash.hexdigest()
        print password,remember
        if password == remember:
            user = Client.objects.filter(client_username=username).first()
            print(user)
            if user:

                return render(request, 'account/client_register.html', {'error': u'用户名已经被注册'})
            else:
                user = Client()
                user.client_username = username
                user.client_password = password
                user.email = email
                user.phone = phone
                user.money = int(money)
                user.gender = gender
                user.qq_number = qq_number
                user.save()
                return redirect('/account/cms_client_login')
        else:
            return render(request, 'account/client_register.html', {'error': u'2次密码不一致'})


def client_login(request):
    if request.method == 'GET':
        return render(request, 'account/client_login.html')
    else:
        form = Client_loginFrom(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username', None)
            password = form.cleaned_data.get('password', None)
            user = Client.objects.filter(client_username=username).first()
            hash = hashlib.md5()
            hash.update(bytes(password, encoding='utf-8'))
            password = hash.hexdigest()
            if user:
                if user.client_password == password:
                    userid = user.id
                    return redirect('/account/cms_user_home/{}'.format(userid))
                else:
                    return render(request, 'account/client_login.html', {'error': u'密码错误'})
            else:
                return render(request, 'account/client_login.html', {'error': u'没有这个用户'})


def personnel_register(request):
    if request.method == 'GET':
        return render(request, 'account/personnel_register.html')
    else:

        username = request.POST.get('username', None)
        phone = request.POST.get('phone', None)
        base_salary = request.POST.get('base_salary', None)
        gender = request.POST.get('gender', None)
        age = request.POST.get('age', None)
        native_place = request.POST.get('native_place', None)
        email = request.POST.get('email', None)
        employee_category = request.POST.get('employee_category', None)
        qq_number = request.POST.get('qq_number', None)
        user = Personnel.objects.filter(username=username).first()
        if user:
            return render(request, 'account/personnel_register.html', {'error': u'用户名已经被注册'})
        else:
            user = Personnel()
            user.username = username
            user.phone = phone
            user.base_salary = base_salary
            user.gender = gender
            user.age = age
            user.email = email
            user.native_place = native_place
            user.qqnumber = qq_number
            user.save()
            personnel_type = Personnel_type()
            personnel_type.personneltype = employee_category
            personnel_type.username = user
            personnel_type.save()
            return redirect('/account/cms_personnel_home')



def modif_password(request,userid=''):
    if request.method == 'GET':
        user = Client.objects.get(pk=userid)
        return render(request, 'account/modify_password.html', {'user': user})
    else:
        new_password = request.POST.get('password', None)
        renew_password = request.POST.get('repassword', None)
        user = Client.objects.get(pk=userid)
        if user:
            if new_password == renew_password:
                user.client_password = new_password
                user.save()
                return redirect('/account/cms_user_home/{}'.format(user.id))
            else:
                return render(request, 'account/modify_password.html', {'error': u'2次密码不一致'})
        else:
            return render(request, 'account/modify_password.html', {'error': u'没有这个用户'})



def reset_password(request):
    if request.method == 'GET':
        return render(request, 'account/repassword.html')
    else:
        form = Reset_passwordFrom(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username', None)
            user = Client.objects.filter(client_username=username).first()
            if user:
                return redirect('/account/cms_client_modif_password', {'user': user.id})
            else:
                return render(request, 'account/repassword.html', {'error': u'没有此用户'})



def userhome(request,userid):

    user = Client.objects.get(pk=userid)
    if request.method == 'GET':
        return render(request, 'account/user_home.html', {'user': user})



def personnel_home(request):
    if request.method == 'GET':
        print '***********************************'
        personnel_users = Personnel.objects.all()
        print personnel_users[0].id
        return render(request, 'account/staff_management.html', {'personnel_users': personnel_users,'user':personnel_users[0]})


def personnel_display(request):
    if request.method == 'GET':
        userid = request.GET.get("userid")
        personnel_user = Personnel.objects.get(pk=userid)
        return JsonResponse({
            "gender": personnel_user.gender,
            "age": personnel_user.age,
            "username": personnel_user.username,
            "wage": personnel_user.wage,
            "phone": personnel_user.phone,
            "email": personnel_user.email,
            "qqnumber": personnel_user.qqnumber,
            "native_place": personnel_user.native_place,
        })



def modifusercenter(request,userid=''):
    if request.method == 'GET':
        user = Client.objects.get(pk=userid)
        print user.gender
        print '***************************************'
        return render(request, 'account/modify_client_user.html', {'user': user})
    else:

        username = request.POST.get('username', None)
        gender = request.POST.get('gender', None)
        phone = request.POST.get('phone', None)
        qqnumber = request.POST.get('qqnumber', None)
        user = Client.objects.filter(client_username=username).first()
        if user:
            user.gender = gender
            user.phone = phone
            user.qqnumber = qqnumber
            user.save()
            return redirect('/account/cms_user_home/{}'.format(user.id))
        else:
            return render(request, 'account/modify_userhome.html', {'error': u'没有此用户'})





def personel_modifhome(request,userid):
    if request.method == 'GET':
        user = Personnel.objects.get(pk=userid)
        return render(request, 'account/modify_personnelhome.html', {'user': user})
    else:

        print '*****************************'
        username = request.POST.get('username', None)
        phone = request.POST.get('phone', None)
        gender = request.POST.get('gender', None)
        age = request.POST.get('age', None)
        native_place = request.POST.get('native_place', None)
        email = request.POST.get('email', None)
        employee_category = request.POST.get('employee_category', None)
        qq_number = request.POST.get('qq_number', None)
        user = Personnel.objects.filter(username=username).first()
        user.username = username
        user.phone = phone
        user.gender = gender
        user.age = age
        user.email = email
        user.native_place = native_place
        user.qqnumber = qq_number
        print user.personnel_user.all()[0].personneltype
        user_type = Personnel_type.objects.filter(username=user).first()
        user_type.personneltype = employee_category
        user_type.save()
        user.save()
        return redirect('/account/cms_personnel_home')






